<html>
<head><meta charset="utf-8"><title>PSA: rowan might be a severe case of over engineering · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html">PSA: rowan might be a severe case of over engineering</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222901655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222901655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222901655">(Jan 15 2021 at 18:23)</a>:</h4>
<p><span class="user-mention" data-user-id="276242">@Riccardo D'Ambrosio</span>  reports that <a href="https://github.com/domenicquirl/cstree">https://github.com/domenicquirl/cstree</a>, a variation of rowan much closer to libsyntax (and to the early versions of rowan!) significantly (2x) outperforms rowan for their use-case, <em>both</em> in speed <strong>and</strong> memory. </p>
<p>I haven't looked into this closely yet. However, I tried building rowan with the thread-local node cache completely disabled... and it <em>is</em> somewhat faster.</p>
<p>This ... makes me question design choices :-)</p>



<a name="222901878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222901878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222901878">(Jan 15 2021 at 18:25)</a>:</h4>
<p>haha, yeah there are some <em>questionable</em> decisions in rowan, i think the biggest of which being cloning and regenerating node data way too much</p>



<a name="222902724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222902724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222902724">(Jan 15 2021 at 18:30)</a>:</h4>
<p>however, there is a drawback in dropping the tree all at once, if anything is holding stray nodes or tokens then the tree will still be kept alive and you may run into memory leaks. For me it isnt a big problem because my analyses are pretty simple but for RA im not sure if the approach will work</p>



<a name="222902886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222902886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222902886">(Jan 15 2021 at 18:31)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="337115">@Domenic Quirl</span> who is the dev of the fork btw</p>



<a name="222911134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222911134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Domenic Quirl <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222911134">(Jan 15 2021 at 19:23)</a>:</h4>
<p>Hello everyone! This comes a bit unexpectedly for me, especially since I just created the repository this week. So maybe first of all be aware that there still are a few things not yet put into place, and that I've been working on this in my free time. I was previously using rowan for my current project, which is more or less building new language tooling for a dynamic DSL, and am very happy with it in terms of the functionality it offers.</p>
<p>That said, I originally started looking into the implementation of tree building, i.e. green trees, as that showed up unexpectedly much in my performance data and I was curious. So a lot of the performance improvements I got from using what has now/will now become <code>cstree</code> stem from changes that happened there before I then went further and also looked at the syntax layer. I am fairly sure there is at least one issue/PR on rowan about only allocating new nodes if they are not deduplicated, and I think <span class="user-mention" data-user-id="276242">@Riccardo D'Ambrosio</span> has a nice picture somewhere of recursive subtree hashing being very visible in his flamegraphs, this is where I started. At some point I brougt in an interner to also deduplicate the token strings, which replaced SmolStr and also had a noticable impact.</p>
<p>Regarding the free list, it was never my primary goal to remove it, except that at one point I had been annoyed that you can't really have generics in the cursor layer. I only looked at it when, after the changes described above, cloning and dropping Rc was the thing that still showed up in benchmarks. So the removal of clones became the introduction of references, which meant the children I want to give out references to have to live somewhere and the tree became persistent, so the free list wasn't used that much anymore. I think it's quite nice that this lets you basically remove the API layer, which has allowed ideas like custom data in tree nodes. However I am not so sure about the touted magnitude of the memory improvements - a lot of them also came from the original changes to the green node allocation and the savings might be somewhat offset by persisting the tree (I know you are aware of that tradeoff, since memoizing red nodes is discussed in the syntax docs). In particular, I don't know how memory performs on very large trees. I have a reasonably large test case, but so far I have only benched parsing (and tree building) and tree traversal speed on that consistently.</p>
<p>Overall I hope it's fine for you that this is now out on the web. I'm happy if this is in any way useful to you, please don't hesitate to ask further questions if you have any!</p>



<a name="222912974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222912974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222912974">(Jan 15 2021 at 19:35)</a>:</h4>
<p><span class="user-mention" data-user-id="337115">@Domenic Quirl</span> do you think interning is required even if tokens are deduplicated? I'd expect that token deduplication to be more or less equivalent to interning. (But SmalStr dosn't make too much sense, the text should just be stored in tokens allocation)</p>



<a name="222914472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222914472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222914472">(Jan 15 2021 at 19:46)</a>:</h4>
<blockquote>
<p>I think @Riccardo D'Ambrosio has a nice picture somewhere of recursive subtree hashing being very visible in his flamegraphs</p>
</blockquote>
<p><a href="/user_uploads/4715/wbxc9e2f5VtAo7_HiV3FjNLH/Screenshot_1882.png">Screenshot_1882.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/wbxc9e2f5VtAo7_HiV3FjNLH/Screenshot_1882.png" title="Screenshot_1882.png"><img src="/user_uploads/4715/wbxc9e2f5VtAo7_HiV3FjNLH/Screenshot_1882.png"></a></div>



<a name="222914611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222914611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Domenic Quirl <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222914611">(Jan 15 2021 at 19:47)</a>:</h4>
<p>Required is a strong word. In my benchmarks it made a 20-25% difference (depending on whether you look at time or throughput) for the whole parsing process. I expect I'm getting above average mileage because the DSL tends to be fairly repetitive for larger inputs (it has "fixed" actions, so no new function definitions and calls etc.), but also the thing with the text is that even if the token is deduplicated and you don't end up _storing_ the text, you still need to make a SmolStr, which gets made into a token, and only _then_ do you check the token against the cache. I.e. the tree sink makes the SmolStr either way, but maybe it gets dropped again.</p>



<a name="222914793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222914793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222914793">(Jan 15 2021 at 19:48)</a>:</h4>
<p>This also shows that rowan methods (my fork which just used an arc instead of an rc) were most of the run time <a href="/user_uploads/4715/GcubP_b9UsSjbHIXmbwDIOSL/Screenshot_1908.png">Screenshot_1908.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/GcubP_b9UsSjbHIXmbwDIOSL/Screenshot_1908.png" title="Screenshot_1908.png"><img src="/user_uploads/4715/GcubP_b9UsSjbHIXmbwDIOSL/Screenshot_1908.png"></a></div><p>The first function is descendants_with_tokens</p>



<a name="222914850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222914850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Domenic Quirl <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222914850">(Jan 15 2021 at 19:48)</a>:</h4>
<p>With the interner, you only "unnecessarily" make a key (or actually intern the string, but only in cases where you will also make a new token), and you also only hash that key I believe instead of the actual string</p>



<a name="222915140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915140">(Jan 15 2021 at 19:50)</a>:</h4>
<p>haha, lol, I figured why thread-local cache dosn't help</p>



<a name="222915170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Domenic Quirl <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915170">(Jan 15 2021 at 19:51)</a>:</h4>
<p>I originally wanted to allow for different text storage options, so the user can choose what he needs. But that's kinda complicated both because of the type erasure (packed pointers) and because the mechanisms are so different</p>



<a name="222915224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915224">(Jan 15 2021 at 19:51)</a>:</h4>
<p>it helds up to 128 node concurrently -- the thinking is that's enough for dfs</p>



<a name="222915305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915305">(Jan 15 2021 at 19:52)</a>:</h4>
<p>the problem is... we do a <strong>b</strong>fs, so therer are 100k nodes. oopsie</p>



<a name="222915346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915346">(Jan 15 2021 at 19:52)</a>:</h4>
<p>haha</p>



<a name="222915656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/PSA%3A%20rowan%20might%20be%20a%20severe%20case%20of%20over%20engineering/near/222915656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/PSA.3A.20rowan.20might.20be.20a.20severe.20case.20of.20over.20engineering.html#222915656">(Jan 15 2021 at 19:54)</a>:</h4>
<p>Yeah for me the biggest impact was iterating over the tree many times, and every time you iterate, every single node and token is cloned. I didn't quite like how rowan did not allow you to have children, descendants, and other things by reference :/</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>